package com.mdlib.helper.ui.fragments.sect.money;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.TextView;

import com.ljlib.core.util.EmptyUtils;
import com.ljlib.help.R;
import com.mdlib.helper.api.DataManager;
import com.mdlib.helper.api.network.RxHelper;
import com.mdlib.helper.api.network.RxSubscribe;
import com.mdlib.helper.base.BaseTitleFragment;
import com.mdlib.helper.model.AccountModel;
import com.mdlib.helper.model.BaseResult;
import com.mdlib.helper.model.entity.OrderEntity;
import com.mdlib.helper.ui.UIHelper;
import com.mdlib.helper.utils.ToastUtil;

import butterknife.Bind;
import butterknife.OnClick;

/**
 * 作者：HQY on 16/11/24 14:03
 * 邮箱：hqy_xz@126.com
 */

public class OrderExceptionFragment extends BaseTitleFragment {


    @Bind(R.id.rb_exception_one)
    RadioButton mRbExceptionOne;
    @Bind(R.id.rb_exception_two)
    RadioButton mRbExceptionTwo;
    @Bind(R.id.rb_exception_three)
    RadioButton mRbExceptionThree;
    @Bind(R.id.rb_exception_four)
    RadioButton mRbExceptionFour;
    @Bind(R.id.et_exceptions_content)
    EditText mEtExceptionsContent;
    @Bind(R.id.tv_goods_not_exception)
    TextView mTvGoodsNotException;
    @Bind(R.id.tv_goods_exception)
    TextView mTvGoodsException;
    @Bind(R.id.ll_goods_paid)
    LinearLayout mLlGoodsPaid;
    private String reason = "其他原因";

    private OrderEntity order;
    private String role = "1";

    public static OrderExceptionFragment newInstance(OrderEntity orderEntity) {
        OrderExceptionFragment fragment = new OrderExceptionFragment();
        Bundle bundle = new Bundle();
        bundle.putSerializable(UIHelper.ORDERIDINFO, orderEntity);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (EmptyUtils.isEmpty(getArguments())) {
            removeFragment();
        }
        order = (OrderEntity) getArguments().getSerializable(UIHelper.ORDERIDINFO);
    }

    @Override
    protected int getContentLayoutId() {
        return R.layout.fragment_order_exception;
    }

    @Override
    protected void initView(View rootView) {
        super.initView(rootView);
        setTitle(R.string.details_order_goods_exception, R.color.white)
                .setTitleBgColor(R.color.generic_view_bg)
                .setImgLeftListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        removeFragment();
                    }
                });
    }


    @OnClick({R.id.rb_exception_one, R.id.rb_exception_two, R.id.rb_exception_three, R.id.rb_exception_four, R.id.tv_goods_not_exception, R.id.tv_goods_exception})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.rb_exception_one:
                reason = mRbExceptionOne.getText().toString();
                mRbExceptionOne.setChecked(true);
                mRbExceptionTwo.setChecked(false);
                mRbExceptionThree.setChecked(false);
                mRbExceptionFour.setChecked(false);
                break;
            case R.id.rb_exception_two:
                reason = mRbExceptionTwo.getText().toString();
                mRbExceptionOne.setChecked(false);
                mRbExceptionTwo.setChecked(true);
                mRbExceptionThree.setChecked(false);
                mRbExceptionFour.setChecked(false);
                break;
            case R.id.rb_exception_three:
                reason = mRbExceptionThree.getText().toString();
                mRbExceptionOne.setChecked(false);
                mRbExceptionTwo.setChecked(false);
                mRbExceptionThree.setChecked(true);
                mRbExceptionFour.setChecked(false);
                break;
            case R.id.rb_exception_four:
                reason = mRbExceptionFour.getText().toString();
                mRbExceptionOne.setChecked(false);
                mRbExceptionTwo.setChecked(false);
                mRbExceptionThree.setChecked(false);
                mRbExceptionFour.setChecked(true);
                break;
            case R.id.tv_goods_not_exception:
                removeFragment();
                break;
            case R.id.tv_goods_exception:
                unusual();
                break;
        }
    }

    private void unusual() {
        if (AccountModel.getInstance().getRoleType().equals("user")) {
            role = "1";
        } else {
            role = "2";
        }
        reason = reason + "   " + mEtExceptionsContent.getText().toString();
        addSubscribe(DataManager.getInstance().getUserApi()
                .unusual(order.getId() + "", order.getUid(), order.getRunId(), role, reason)
                .compose(RxHelper.<BaseResult>rxSchedulerHelper())
                .subscribe(new RxSubscribe<BaseResult>() {
                    @Override
                    protected void onSucc(BaseResult baseResult) {
                        ToastUtil.showToast(baseResult.msg);
                        getActivity().finish();
                    }

                    @Override
                    protected void onFail(String msg) {
                        ToastUtil.showToast(msg);
                    }
                }));
    }

}
